<template>
  <su-page bg="#f7f7f7">
    <su-body :end="listAllLoaded && listTotal">
      <!-- <div class="p10 white-bg">
        <u-search v-model="queryParams.name" placeholder="输入企业名称搜索" :showAction="false" @search="reloadlist"></u-search>
      </div> -->
      <!-- 筛选 -->
      <su-filter-bar stickyTop="0" padding="0 5px">
        <u-tabs :list="tabList" :current="queryParams.category" keyName="name" @change="reloadlist"></u-tabs>
        <view class="su-filter-bar__btn-list" slot="asider">
          <su-filter-form :options="filterFormOptions" @confirm="filterFormConfirm"></su-filter-form>
        </view>
      </su-filter-bar>
      <!-- list -->
      <view  class="p10" v-if="tabList.values(1)">
        <su-grant-card v-for="(item,index) in list" :key="index" :data="item"></su-grant-card>
      </view>
      <!-- loading -->
      <su-list-loading :loading="listLoading" :listLength="list.length"></su-list-loading>
      <!-- 无数据 -->
      <u-empty v-if="listAllLoaded && listTotal==0" class="mt40" mode="data"></u-empty>
      
    </su-body>
  </su-page>
</template>

<script>
  /**
   * @description 审批
   * @date 2022-03-12
   * @version v1.0.1
   * @author gf
   */
let tabs="";
  import {
    mapState
  } from 'vuex'
import { runSQL } from '../../../common/utils/wxutils';
   import SuGrantCard from './components/su-grant-card.vue'
  export default {
    components: {
       SuGrantCard
    },
    data() {
      return {
        filterFormOptions: [{
            label: '项目经理',
            prop: 'assetCode',
            placeholder: '请输入项目经理搜索',
            defaultValue: '',
            clearable: true,
            type: 'input'
          },
          {
            label: '签到时间',
            prop: 'createTime',
            placeholder: '请选择签到时间',
            defaultValue: '',
            clearable: true,
            type: 'date'
          },
          // {
          //   label: '状态',
          //   prop: 'sortId',
          //   placeholder: '请选择状态筛选',
          //   defaultValue: '',
          //   type: 'select',
          //   clearable: true,
          //   options: [{
          //       label: '已签到',
          //       value: '1'
          //     },
          //     {
          //       label: '未签到',
          //       value: '2'
          //     },
          //     {
          //       label: '缺勤',
          //       value: '3'
          //     },
          //   ]
          // }
        ],
        tabList: [{
            value: '1',
            name: '已签到'
          },
          {
            value: '2',
            name: '未签到',
          },
        ],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          category: 0,
          key: null,
          tenantId: null,
          deployTime: null,
          derivedFrom: null,
          derivedFromRoot: null,
          parentDeploymentId: null,
          engineVersion: null
        },
        searchKey: '',
        list: [],
        listTotal: 0,
        listAllLoaded: false,
        listLoading: false,
      }
    },
    computed: {
      ...mapState(['token'])
    },
    onLoad(params) {
      this.reloadlist()
    },
    watch: {
      token(newVal) {
        this.reloadlist()
      }
    },
    methods: {
      // 筛选
      filterFormConfirm(formData){
        this.queryParams = {
          ...this.queryParams,
          ...formData
        }
	
        this.reloadlist(tabs,formData)
      },
      // 新增按钮点击
      createNew(id){
        console.log('新增')
        this.$u.route({
          url:'/pages/main/grant/form/form',
          params:{
            id:id
          }
        })
      },
      //初始化参数，重新加载列表
      reloadlist(stopPullDownRefresh,tiaojian) {
        this.listLoading = false
        this.listAllLoaded = false
        this.list = []
        this.listTotal = 0
        this.queryParams.pageNum = 1
        this.getlist(stopPullDownRefresh,tiaojian)
      },
      //获取列表数据
      getlist(stopPullDownRefresh,tiaojian) {
		  let sql="";
		  if(stopPullDownRefresh==""||stopPullDownRefresh==null||stopPullDownRefresh==undefined){
			   let tjcx="and 1=1"
			  if(tiaojian!=""&&tiaojian!=null&tiaojian!=undefined){
				  if(tiaojian.assetCode!=""&&tiaojian.assetCode!=null&&tiaojian.assetCode!=undefined){
					  tjcx=tjcx+"and a.xmjl='"+tiaojian.assetCode+"'"
				  }
				  if(tiaojian.createTime!=""&&tiaojian.createTime!=null&&tiaojian.createTime!=undefined){
					  let ss=tiaojian.createTime.split("/")
					  tjcx=tjcx+"and a.qdnf="+ss[0]+" and a.qdyf="+ss[1]+" and a.qdday="+ss[2]+""
				  }
			  	sql="select recid,xmjl,lxfs,xmmc from ssnydw.wsqd a,(select * from ssnydw.tbhuman where qyryrecid is not null and unitid=(select unitid from ssnydw.tbhuman where humanid="+this.$store.state.token+"))b where a.humanid=b.humanid "+tjcx+";";
			  }else{
				  tjcx=tjcx+"and a.qdnf=extract(year from now()) and a.qdyf=extract(month from now()) and a.qdday=extract(day from now())"
				sql="select recid,xmjl,lxfs,xmmc from ssnydw.wsqd a,(select * from ssnydw.tbhuman where qyryrecid is not null and unitid=(select unitid from ssnydw.tbhuman where humanid="+this.$store.state.token+"))b where a.humanid=b.humanid "+tjcx+";";  
			  }
			  
		  }
		  else{
			  tabs=stopPullDownRefresh;
			
			  if(stopPullDownRefresh.name=="未签到"){
				  let tjcx=" 1=1"
				  if(tiaojian!=""&&tiaojian!=null&tiaojian!=undefined){
				  				  if(tiaojian.assetCode!=""&&tiaojian.assetCode!=null&&tiaojian.assetCode!=undefined){
				  					  tjcx=tjcx+"and xmjl='"+tiaojian.assetCode+"'"
				  				  }
				  				  if(tiaojian.createTime!=""&&tiaojian.createTime!=null&&tiaojian.createTime!=undefined){
				  					  let ss=tiaojian.createTime.split("/")
				  					  tjcx=tjcx+"and qdnf="+ss[0]+" and qdyf="+ss[1]+" and qdday="+ss[2]+""
				  				  }
				  	sql="select 0 as recid,humanname xmjl,cellphone lxfs,'' xmmc  from ssnydw.tbhuman where qyryrecid is not null and unitid=(select unitid from ssnydw.tbhuman where humanid="+this.$store.state.token+") and humanid not in(select humanid from ssnydw.wsqd where "+tjcx+") ;";
				  }else{
				  	tjcx=tjcx+"and qdnf=extract(year from now()) and qdyf=extract(month from now()) and qdday=extract(day from now())"
				  	sql="select 0 as recid,humanname xmjl,cellphone lxfs,'' xmmc  from ssnydw.tbhuman where qyryrecid is not null and unitid=(select unitid from ssnydw.tbhuman where humanid="+this.$store.state.token+") and humanid not in(select humanid from ssnydw.wsqd where "+tjcx+") ;";  
				  }
				  
			  }
			   
			  else{
				let tjcx="and 1=1"
				if(tiaojian!=""&&tiaojian!=null&tiaojian!=undefined){
								  if(tiaojian.assetCode!=""&&tiaojian.assetCode!=null&&tiaojian.assetCode!=undefined){
									  tjcx=tjcx+"and a.xmjl='"+tiaojian.assetCode+"'"
								  }
								  if(tiaojian.createTime!=""&&tiaojian.createTime!=null&&tiaojian.createTime!=undefined){
									  let ss=tiaojian.createTime.split("/")
									  tjcx=tjcx+"and a.qdnf="+ss[0]+" and a.qdyf="+ss[1]+" and a.qdday="+ss[2]+""
								  }
					sql="select recid,xmjl,lxfs,xmmc from ssnydw.wsqd a,(select * from ssnydw.tbhuman where qyryrecid is not null and unitid=(select unitid from ssnydw.tbhuman where humanid="+this.$store.state.token+"))b where a.humanid=b.humanid "+tjcx+";";
				}else{
					tjcx=tjcx+"and a.qdnf=extract(year from now()) and a.qdyf=extract(month from now()) and a.qdday=extract(day from now())"
					sql="select recid,xmjl,lxfs,xmmc from ssnydw.wsqd a,(select * from ssnydw.tbhuman where qyryrecid is not null and unitid=(select unitid from ssnydw.tbhuman where humanid="+this.$store.state.token+"))b where a.humanid=b.humanid "+tjcx+";";  
				}  
			  }
		  }
		  let list=[];
		  runSQL(sql).then(ress=>{
			  for(let i=0;i<ress[0].length;i++){
			  					  
			  					list.push({
			  						deployId:ress[0][i].RECID,
			  						xmjl:ress[0][i].XMJL,
			  						lxfs:ress[0][i].LXFS,
			  						xmmc:ress[0][i].XMMC,
			  					})  
								if (this.listLoading || this.listAllLoaded) return
								
								this.listLoading = true
								setTimeout(()=>{
								  let res = {
								    data:{
								      records:list,
								      total:5
								    }
								  }
								  this.dataLoadedHandler(res, stopPullDownRefresh)
								},200)
								}
			  
		  })
		  
        
      },
      // 列表请求成功后处理
      dataLoadedHandler(res, stopPullDownRefresh) {
      
        this.listLoading = false
        const resData = res.data || {}
        const pageData = resData.records || []
        this.list = this.list.concat(pageData)
        this.listTotal = resData.total || 0
        if (pageData.length < this.queryParams.pageSize) {
          this.listAllLoaded = true
        } else {
          this.queryParams.pageNum++
        }
        if (stopPullDownRefresh) {
          uni.stopPullDownRefresh()
        }
      },
    },

    onPullDownRefresh() {
      this.queryParams.name = ''
      this.reloadlist(true)
    },
    onReachBottom() {
      console.log('触底')
      this.getlist()
    }
  }
</script>

<style lang="scss">
</style>
